package com.soft.admin.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.soft.admin.dto.RoleDTO;
import com.soft.admin.dto.UserDTO;
import com.soft.admin.service.UserService;
import com.soft.model.Role;
import com.soft.model.User;
import com.soft.model.UserRole;
import com.soft.support.PageParam;
import com.soft.support.Pager;
import com.soft.util.DateUtil;
import com.soft.util.JsonUtil;

@SuppressWarnings({"unchecked"})
@Component
public class UserServiceImpl extends BaseServiceImpl implements UserService {

	@Transactional(readOnly = true)
	public String get(UserDTO params, PageParam page) {
		List<Criterion> values = new ArrayList<Criterion>();
		values.add(Restrictions.like("username", params.getUsername(), MatchMode.ANYWHERE));
		values.add(Restrictions.like("truename", params.getTruename(), MatchMode.ANYWHERE));
		values.add(Restrictions.not(Restrictions.eq("username", "admin")));
		Pager userLst = this.getBaseDao().findForPager(User.class, 
				new Order[] { Order.desc("id") }, 
				page.getStart(), page.getLimit(), values);
		Pager pageModel = new Pager();
		pageModel.setTotal(userLst.getTotal());
		List<UserDTO> dtos = new ArrayList<UserDTO>();
		for (User user : (List<User>)userLst.getDatas()) {
			UserDTO dto = new UserDTO();
			BeanUtils.copyProperties(user, dto, new String[]{"loginTime"});
			dto.setLoginTime(DateUtil.format(user.getLoginTime()));
			dtos.add(dto);
		}
		pageModel.setDatas(dtos);
		return JsonUtil.toStringFromObject(pageModel);
	}

	@Transactional
	public String addOrEdit(UserDTO params) {
		User user = null;
		if (params.getId() == null) {
			user = new User();
			user.setLoginTime(new Date());
		} else {
			user = (User)this.getBaseDao().get(User.class, params.getId());
		}
		if (!"".equals(params.getUsername().trim())) {
			if (!params.getUsername().equals(user.getUsername())) {
				boolean isExist = this.getBaseDao().isPropertyExist(User.class, "username", params.getUsername());
				if (isExist) {
					return JsonUtil.toRes("保存失败！", "用户名已存在！");
				}
			}
		} else {
			return JsonUtil.toRes("保存失败！", "用户名不合法！");
		}
		user.setUsername(params.getUsername());
		user.setTruename(params.getTruename());
		user.setEnabled(params.getEnabled());
		user.setPassword(params.getPassword());
		user.setUsername(params.getUsername());
		this.getBaseDao().save(user);
		return JsonUtil.toRes("保存成功！");
	}

	@Transactional
	public String remove(Integer id) {
		this.getBaseDao().delete(User.class, id);
		return JsonUtil.toRes("删除成功！");
	}

	@Transactional(readOnly = true)
	public String getRole() {
 		List<Role> roles = this.getBaseDao().findAll(Role.class, new Order[]{Order.desc("id")},
				Restrictions.eq("level", 0));
		List<RoleDTO> dtos = new ArrayList<RoleDTO>();
		for (Role role : roles) {
			RoleDTO dto = new RoleDTO();
			BeanUtils.copyProperties(role, dto);
			dtos.add(dto);
		}
		return JsonUtil.toString(dtos);
	}

	@Transactional(readOnly = true)
	public String getRoleByUser(Integer id) {
		UserRole ur = (UserRole)this.getBaseDao().findUniqueByProperty(UserRole.class, "user.id", id);
		if(ur == null) {
			return JsonUtil.toRes("");
		} else {
			return JsonUtil.toRes(ur.getRole().getId().toString());
		}
	}

	@Transactional
	public String config(Integer id, Integer roleId) {
		UserRole ur = (UserRole)this.getBaseDao().findUniqueByProperty(UserRole.class, "user.id", id);
		if (!ur.getRole().getId().equals(roleId)) {
			Role role = (Role)this.getBaseDao().get(Role.class, roleId);
			ur.setRole(role);
			this.getBaseDao().saveOrUpdate(ur);
		}
		return JsonUtil.toRes("保存成功！");
	}
	
}
